var http = require('../../../utils/https.js');
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data: {

    codeImg: "…vhv8A4qo7nxnqN1azW7w2oSVGRiqtkAjHHzVMq0bMUqsbM//Z",
    imgUrl: "data:image/png;base64,",
    icnoP: 'https://wqqqf.oss-cn-beijing.aliyuncs.com/921416cd12798ad85b0d542ff86bf7bb.png',
    icnoC: 'https://wqqqf.oss-cn-beijing.aliyuncs.com/7ff3df4d3bdf4f653fa2f39499169c6d.png',
    uuid: '',
    token: '',

    username: '',
    error_username:'',
    nickName:'',
    error_nickName:'',
    password: '',
    error_password:'',
    password2: '',
    error_password2:'',
    code: '',
    error_code:'',
    
  },
  isAlphanumeric(value) {
    const reg = /^[A-Za-z0-9]+$/;
    return reg.test(value);
  },
  updateCode() {
    var img = 'data:image/png;base64,'
    var t = Date.now()
    var that = this
    http.GET('/captchaImage', {}, function (res) {
      img = img + res.img
      that.setData({
        imgUrl: img,
        uuid: res.uuid
      })
      console.log('成功:', res);
    }, function (err) {
      console.error('失败:', err);
    });
  },
  isUserName(event) {
    var username = event.detail.value;
    if (username.trim() === '') {
      this.setData({
        error_username: '账号不能为空'
      });
    } else {
      this.setData({
        error_username: ''
      });
    }
  },
  isNickName(event) {
    var nickName = event.detail.value;
    if (nickName.trim() === '') {
      this.setData({
        error_nickName: '姓名不能为空'
      });
    } else{
      this.setData({
        error_nickName:''
      })
    }
  },
  isPassword(event) {
    var password = event.detail.value;
    if (password.trim() === '') {
      this.setData({
        error_password: '密码不能为空'
      });
    } else{
      this.setData({
        error_password:''
      })
    }
  },
  isPassword2(event) {
    var password2 = event.detail.value;
    if (password2.trim() === '') {
      this.setData({
        error_password2: '密码不能为空'
      });
    } else{
      if(password2!=this.data.password){
        this.setData({
          error_password2:'密码不一致'
        })
      }else{
        this.setData({
          error_password2:''
        })
      }
      
    }
  },

  isCode(event) {
    var code = event.detail.value;
    if (code.trim() === '') {
      this.setData({
        error_code: '验证码不能为空'
      });
    } else{
      this.setData({
        error_code:''
      })
    }
  },

  onChangeU(event) {
    this.setData({
      username: event.detail
    })
  },
  onChangeA(event) {
    // event.detail 为当前输入的值
    this.setData({
      nickName: event.detail
    })
  },
  onChangeP2(event) {
    this.setData({
      password2: event.detail
    })
  },
  onChangeP(event) {
    this.setData({
      password: event.detail
    })
  },

  onChangeC(event) {
    this.setData({
      code: event.detail
    })
  },
  register(){

    var username = this.data.username
    var nickName = this.data.nickName
    var password = this.data.password
    var password2 = this.data.password2
    var code = this.data.code
    var uuid = this.data.uuid

    if(this.isAlphanumeric(username)&&nickName!=''&&this.isAlphanumeric(password)&&this.isAlphanumeric(password2)&&this.isAlphanumeric(code)&&this.data.password==this.data.password2){
      http.POST('/register', {
        code:code,
        password:password,
        username:username,
        uuid:uuid,
        nickName:nickName,
        confirmPassword:password2
      }, function(res) {
        if(res.code===200){
          wx.showToast({
            title: '注册成功',
            icon:'success',
            duration: 1500
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '/index/pages/mzhLogin/mzhLogin',
            })
          }, 1500);

        }else{
          wx.showToast({
            title: res.msg,
            icon:'error'
          })
        }
      }, function(err) {
        console.error('失败:', err);
      });
    }else{
      wx.showToast({
        title: '请认真填写',
        icon:'error'
      })
    }
  },
  onLoad(options) {
    this.updateCode()
  },
})